1. Считайте файл city.csv.
  2. Переименуйте все колонки в верхний регистр.
  3. Проверьте, в каких колонках есть отсутствующие значения.
  4. Удалите из датафрейма строки с отсутствующими значениями.
  5. Выберите в новый датафрейм строки с населением более 1 млн.
  6. Удалите столбец DISTRICT.
  7. Запишите датафрейм в файл test.xlsx без сохранения индексов.
In [1]:
import pandas as pd
In [24]:
df = pd.read_csv('city.csv', sep=';')
df
Out[24]:
ID Name CountryCode District Population
0 1 Kabul AFG Kabol 1780000
1 2 Qandahar AFG Qandahar 237500
2 3 Herat AFG Herat 186800
3 4 Mazar-e-Sharif AFG Balkh 127800
4 5 Amsterdam NLD Noord-Holland 731200
... ... ... ... ... ...
4074 4075 Khan Yunis PSE Khan Yunis 123175
4075 4076 Hebron PSE Hebron 119401
4076 4077 Jabaliya PSE North Gaza 113901
4077 4078 Nablus PSE Nablus 100231
4078 4079 Rafah PSE Rafah 92020

4079 rows × 5 columns

In [25]:
df.rename(str.upper, axis=1, inplace=True)
In [26]:
df
Out[26]:
ID NAME COUNTRYCODE DISTRICT POPULATION
0 1 Kabul AFG Kabol 1780000
1 2 Qandahar AFG Qandahar 237500
2 3 Herat AFG Herat 186800
3 4 Mazar-e-Sharif AFG Balkh 127800
4 5 Amsterdam NLD Noord-Holland 731200
... ... ... ... ... ...
4074 4075 Khan Yunis PSE Khan Yunis 123175
4075 4076 Hebron PSE Hebron 119401
4076 4077 Jabaliya PSE North Gaza 113901
4077 4078 Nablus PSE Nablus 100231
4078 4079 Rafah PSE Rafah 92020

4079 rows × 5 columns

In [27]:
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4079 entries, 0 to 4078
Data columns (total 5 columns):
 #   Column       Non-Null Count  Dtype 
---  ------       --------------  ----- 
 0   ID           4079 non-null   int64 
 1   NAME         4079 non-null   object
 2   COUNTRYCODE  4079 non-null   object
 3   DISTRICT     4075 non-null   object
 4   POPULATION   4079 non-null   int64 
dtypes: int64(2), object(3)
memory usage: 159.5+ KB
In [28]:
df[ df.DISTRICT.isna() ]
Out[28]:
ID NAME COUNTRYCODE DISTRICT POPULATION
3284 3285 Taiping TWN NaN 165524
3292 3293 Taliao TWN NaN 115897
3293 3294 Kueishan TWN NaN 112195
3562 3563 Ciudad Losada VEN NaN 134501
In [29]:
df.dropna(inplace=True)
In [30]:
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 4075 entries, 0 to 4078
Data columns (total 5 columns):
 #   Column       Non-Null Count  Dtype 
---  ------       --------------  ----- 
 0   ID           4075 non-null   int64 
 1   NAME         4075 non-null   object
 2   COUNTRYCODE  4075 non-null   object
 3   DISTRICT     4075 non-null   object
 4   POPULATION   4075 non-null   int64 
dtypes: int64(2), object(3)
memory usage: 191.0+ KB
In [31]:
df = df[ df.POPULATION > 1000000 ].copy()
In [32]:
df.drop('DISTRICT', axis=1, inplace=True)
In [33]:
df
Out[33]:
ID NAME COUNTRYCODE POPULATION
0 1 Kabul AFG 1780000
34 35 Alger DZA 2168000
55 56 Luanda AGO 2022000
68 69 Buenos Aires ARG 2982146
69 70 La Matanza ARG 1266461
... ... ... ... ...
3797 3798 Phoenix USA 1321045
3798 3799 San Diego USA 1223400
3799 3800 Dallas USA 1188580
3800 3801 San Antonio USA 1144646
4067 4068 Harare ZWE 1410000

237 rows × 4 columns

In [34]:
df.to_excel('test.xlsx', index=False)
In [ ]:
 
In [35]:
df2 = pd.DataFrame({
    'country': ['France', 'USA', 'Ukraine'],
    'capital': ['Paris', 'Washington', 'Kiev'],
    'population': [66.99, 328.2, 41.98]
})
df2
Out[35]:
country capital population
0 France Paris 66.99
1 USA Washington 328.20
2 Ukraine Kiev 41.98
In [44]:
with pd.ExcelWriter('cities2.xlsx') as writer:
    df2.to_excel(writer, sheet_name='TestSheet', index=False)
    df.to_excel(writer, sheet_name='TestSheet', index=False, startrow=(len(df2) + 6))
In [40]:
df.shape[0]
Out[40]:
237
In [41]:
len(df)
Out[41]:
237
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]: